/*
 * nlsync
 * Copyright (c) 2010 apechurin 
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
using System;
using System.Collections.Generic;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;

namespace nlsync.Xls
{
    /// <summary>
    /// Implementation of the IStringInfo interface for a Xls based 
    /// implementation. Note that removing of another string can influence this 
    /// one so it's not recommended and XlsStringInfo objects should not be used
    /// after remove.
    /// </summary>
    class XlsStringInfo : IStringInfo
    {
        #region Constructors

        /// <summary>
        /// Constructs a new string info. Used to represent a string which is
        /// already defined in Excel.
        /// </summary>
        /// <param name="row">The row.</param>
        /// <param name="sheet">The sheet.</param>
        public XlsStringInfo(int row, XlsSheet sheet)
        {
            mSheet = sheet;
            mIsNew = false;
            mRow = row;
        }

        /// <summary>
        /// Constructs a new string info. Used for newly added strings in Excel.
        /// </summary>
        /// <param name="bundle">The parent bundle.</param>
        /// <param name="source">The string source.</param>
        /// <param name="sheet">The sheet.</param>
        public XlsStringInfo(XlsSheet sheet, string source, string name)
        {
            mSheet = sheet;
            mIsNew = true;
            mRow = mSheet.AddRow();

            mSheet.SetValue(mRow, SourceColumn, source);
            mSheet.SetValue(mRow, NameColumn, name);
        }

        #endregion

        #region IStringInfo methods

        public string Name
        {
            get
            {
                return mSheet.GetValue(mRow, NameColumn);
            }
        }

        public string DefaultValue
        {
            get
            {
                return mSheet.GetValue(mRow, DefaultValueColumn);
            }
            set
            {
                Excel.Range cell = mSheet.SetValue(mRow, DefaultValueColumn, value);
                cell.Interior.Color = CellColor;
            }
        }

        public string Value
        {
            get
            {
                return mSheet.GetValue(mRow, ValueColumn);
            }
            set
            {
                mSheet.SetValue(mRow, ValueColumn, value);
            }
        }

        public string Comment
        {
            get
            {
                return mSheet.GetValue(mRow, CommentColumn);
            }
            set
            {
                Excel.Range cell = mSheet.SetValue(mRow, CommentColumn, value);
                cell.Interior.Color = CellColor;
            }
        }

        #endregion

        #region Internal methods

        /// <summary>
        /// Removes the string info. The object cannot be used anymore after 
        /// the call.
        /// </summary>
        /// <returns>The removed row number.</returns>
        internal int Remove()
        {
            mSheet.RemoveRow(mRow);
            mSheet = null;
            int t = mRow;
            mRow = -1;
            return t;
        }

        /// <summary>
        /// Gets the source (aka bundle name).
        /// </summary>
        internal string Source
        {
            get
            {
                return mSheet.GetValue(mRow, SourceColumn);
            }
        }

        // for backward compatibility with unit tests
        internal Excel.Range Range
        {
            get
            {
                Excel.Worksheet sheet = mSheet.Worksheet;
                
                string address = string.Format("$A${0}", mRow);
                Excel.Range cell = sheet.get_Range(address, Type.Missing);
                
                return cell.EntireRow.Cells;
            }
        }

        #endregion

        #region Implementation

        private int CellColor
        {
            get
            {
                return mIsNew ? RGB(255, 255, 0) : RGB(255, 0, 0);
            }
        }

        private int RGB(int r, int g, int b)
        {
            return (b << 16 | g << 8 | r);
        }

        #endregion

        #region Data fields

        /// <summary>
        /// The corresponding row.
        /// </summary>
        private int mRow;

        /// <summary>
        /// Is it a new row or an old one
        /// </summary>
        bool mIsNew;

        /// <summary>
        /// The sheet.
        /// </summary>
        XlsSheet mSheet;

        // columns
        internal static readonly int SourceColumn = 1;
        internal static readonly int NameColumn = 2;
        internal static readonly int DefaultValueColumn = 3;
        internal static readonly int ValueColumn = 4;
        internal static readonly int CommentColumn = 5;

        #endregion
    }
}
